package com.example.tianling.service.Impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.example.tianling.common.vo.article;
import com.example.tianling.common.vo.bookListVo;
import com.example.tianling.dao.BookMapper;
import com.example.tianling.dao.UserBookMarksMapper;
import com.example.tianling.entity.*;
import com.example.tianling.service.BookService;
import com.example.tianling.service.UserBookMarksService;
import com.github.yulichang.wrapper.MPJLambdaWrapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@Service
public class UserBookMarksServiceImpl
        extends ServiceImpl<UserBookMarksMapper, UserBookMarksEntity>
        implements UserBookMarksService {
    @Autowired
    private UserBookMarksMapper userBookMarksMapper;
    @Override
    public Integer setBookMark(UserBookMarksEntity userBookMarksEntity,Integer userId) throws ParseException {
        QueryWrapper<UserBookMarksEntity> materialCollectionsEntityQueryWrapper = new QueryWrapper<>();
        materialCollectionsEntityQueryWrapper.eq("book_id",userBookMarksEntity.getBookId());
        materialCollectionsEntityQueryWrapper.eq("user_id",userId);
        if(userBookMarksMapper.selectOne(materialCollectionsEntityQueryWrapper)!=null){
            return -2;
        }
        SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        String currentDateTimeString = formatter.format(new Date(System.currentTimeMillis()));
        userBookMarksEntity.setBookMarkTime(formatter.parse(currentDateTimeString));
        userBookMarksEntity.setUserId(userId);
        return userBookMarksMapper.insert(userBookMarksEntity);
    }

    @Override
    public List<bookListVo> getMyBookMark(Integer userId,Integer pageNum, String type){
        Page<bookListVo> pg = new Page<>(pageNum, 10);

        IPage<bookListVo> bookListVoIPage = userBookMarksMapper.selectJoinPage(pg,bookListVo.class,
                new MPJLambdaWrapper<bookListVo>()
                        .select(BookEntity::getBookId)
                        .select(BookEntity::getBookName)
                        .select(BookEntity::getYear)
                        .select(BookEntity::getArea)
                        .select(BookEntity::getContent)
                        .select(BookEntity::getIsNeedVip)
                        .likeLeft(BookEntity::getType,type)
                        .likeRight(BookEntity::getType,type)
                        .eq(UserBookMarksEntity::getUserId,userId)
                        .leftJoin(BookEntity.class,BookEntity::getBookId,UserBookMarksEntity::getBookId)
                        .orderByDesc(UserBookMarksEntity::getBookMarkTime));
        for(bookListVo bookListVo:bookListVoIPage.getRecords()){
            bookListVo.setContent(bookListVo.getContent().substring(0,20));
        }
        return bookListVoIPage.getRecords();
    }

    @Override
    public Integer cancelMyBookCollection(Integer userId ,Integer bookId){
        QueryWrapper<UserBookMarksEntity> materialCollectionsEntityQueryWrapper = new QueryWrapper<>();
        materialCollectionsEntityQueryWrapper.eq("book_id",bookId);
        materialCollectionsEntityQueryWrapper.eq("user_id",userId);
        return userBookMarksMapper.delete(materialCollectionsEntityQueryWrapper);
    }
    public Integer iscollectBook(Integer userId ,Integer bookId){
        QueryWrapper<UserBookMarksEntity> materialCollectionsEntityQueryWrapper = new QueryWrapper<>();
        materialCollectionsEntityQueryWrapper.eq("book_id",bookId);
        materialCollectionsEntityQueryWrapper.eq("user_id",userId);
        if(userBookMarksMapper.selectCount(materialCollectionsEntityQueryWrapper)==0){
            return 0;
        }
        else{
            return 1;
        }
    }
}
